Imports System.Text.RegularExpressions

Namespace Main.Forms
    Public Class FmRules
#Region "Declares"
        Private _mFileName As String
        Public Property FileName() As String
            Get
                Return _mFileName
            End Get
            Set(ByVal value As String)
                _mFileName = value
            End Set
        End Property

        Private _mRegex As String
        Public Property SRegex() As String
            Get
                Return _mRegex
            End Get
            Set(ByVal value As String)
                _mRegex = value
            End Set
        End Property

        Private _mReplace As String
        Public Property SReplace() As String
            Get
                Return _mReplace
            End Get
            Set(ByVal value As String)
                _mReplace = value
            End Set
        End Property

#End Region

#Region "Form Events"

        Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
            _mRegex = txtRegex.Text
            _mReplace = txtReplace.Text
            DialogResult = DialogResult.OK
        End Sub

        Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
            DialogResult = DialogResult.Cancel
        End Sub

        Private Sub txtReplace_TextChanged(sender As Object, e As EventArgs) Handles txtReplace.TextChanged, txtRegex.TextChanged
            'http://www.regular-expressions.info/reference.html
            If (txtRegex.Text.Length > 0) Then
                Dim rx As Regex = New Regex(txtRegex.Text)
                txtPreview.Text = rx.Replace(txtOrigin.Text, txtReplace.Text)
            Else
                Try
                    Dim bCustom As Boolean = False
                    Dim strResult As String = txtReplace.Text
                    If (txtReplace.Text.IndexOf("%f", StringComparison.Ordinal) >= 0) Then
                        strResult = strResult.Replace("%f", txtOrigin.Text.Substring(0, txtOrigin.Text.LastIndexOf(".")))
                        bCustom = True
                    End If
                    If (txtReplace.Text.IndexOf("%e", StringComparison.Ordinal) >= 0) Then
                        Dim dpos As Integer = txtOrigin.Text.LastIndexOf(".") + 1
                        strResult = strResult.Replace("%e", txtOrigin.Text.Substring(dpos, txtOrigin.Text.Length - dpos))
                        bCustom = True
                    End If
                    If (txtReplace.Text.IndexOf("%d", StringComparison.Ordinal) >= 0) Then
                        strResult = strResult.Replace("%d", "01")
                        bCustom = True
                    End If

                    If (Not bCustom) And (txtRegex.Text.Length > 0) Then
                        Dim rx As Regex = New Regex(txtRegex.Text)
                        txtPreview.Text = rx.Replace(txtOrigin.Text, txtReplace.Text)
                    Else
                        txtPreview.Text = strResult
                    End If
                Catch ex As Exception
                    txtPreview.Text = txtOrigin.Text
                End Try
            End If

            'If (txtReplace.Text.Length > 0) Then
            '    Try
            '        Dim bCustom As Boolean = False
            '        Dim strResult As String = txtReplace.Text
            '        If (txtReplace.Text.IndexOf("%f", StringComparison.Ordinal) >= 0) Then
            '            strResult = strResult.Replace("%f", txtOrigin.Text.Substring(0, txtOrigin.Text.LastIndexOf(".")))
            '            bCustom = True
            '        End If
            '        If (txtReplace.Text.IndexOf("%e", StringComparison.Ordinal) >= 0) Then
            '            Dim dpos As Integer = txtOrigin.Text.LastIndexOf(".") + 1
            '            strResult = strResult.Replace("%e", txtOrigin.Text.Substring(dpos, txtOrigin.Text.Length - dpos))
            '            bCustom = True
            '        End If
            '        If (txtReplace.Text.IndexOf("%d", StringComparison.Ordinal) >= 0) Then
            '            strResult = strResult.Replace("%d", "01")
            '            bCustom = True
            '        End If

            '        If (Not bCustom) And (txtRegex.Text.Length > 0) Then
            '            Dim rx As Regex = New Regex(txtRegex.Text)
            '            txtPreview.Text = rx.Replace(txtOrigin.Text, txtReplace.Text)
            '        Else
            '            txtPreview.Text = strResult
            '        End If
            '    Catch ex As Exception
            '    End Try
            'Else
            '    txtPreview.Text = txtOrigin.Text
            'End If
        End Sub

        Private Sub fmRules_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
            txtOrigin.Text = _mFileName
            txtPreview.Text = txtOrigin.Text
            txtReplace.Focus()
        End Sub
#End Region

    End Class
End Namespace
